package quotation.web.personDay.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import org.apache.commons.jexl3.JexlBuilder;
import org.apache.commons.jexl3.JexlContext;
import org.apache.commons.jexl3.JexlEngine;
import org.apache.commons.jexl3.MapContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import quotation.R.R;
import quotation.web.personDay.ddd.entity.PersonDayEntity;
import quotation.web.personDay.ddd.form.SavePersonForm;
import quotation.web.personDay.service.PersonDayService;

import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author lyx
 * @date 2025/2/19
 */

@RestController
@RequestMapping("/personDay")
@Api(tags = "人天管理")
public class PersonDayController {


    @Autowired
    private PersonDayService personDayService;

    @PostMapping("/saveAll")
    @ApiModelProperty(value = "保存人天管理")
    public R saveAll(@RequestBody @Valid SavePersonForm form) {

        personDayService.saveAll(form);
        return R.success("新增成功");
    }

    @DeleteMapping("/delete")
    @ApiModelProperty(value = "删除人天管理")
    public R remove() {
        return R.success("删除成功");
    }

    @GetMapping("/get")
    @ApiModelProperty(value = "获取人天管理")
    public R get() {
        List<PersonDayEntity> personDayList = personDayService.getAll();
        Map<String, List<PersonDayEntity>> groupedList = personDayList.stream()
                .collect(Collectors.groupingBy(
                        PersonDayEntity::getName,
                        Collectors.collectingAndThen(
                                Collectors.toList(),
                                list -> list.stream()
                                        .sorted(Comparator.comparingInt(PersonDayEntity::getLowerLimit))
                                        .collect(Collectors.toList())
                        )
                ));
        return R.success("获取成功",groupedList);
    }

    public static void main(String[] args) {
        //String expressionString = "总人数+一线生产岗位人员-普通销售岗位人员+客服、后勤保障人员+ljj-dkw";
        String expressionString = "(lyx+ljj)*0.1-dkw%11";
        JexlEngine jexlEngine = new JexlBuilder().create();
        // 定义上下文
        JexlContext context = new MapContext();
        context.set("lyx", 10);
        context.set("ljj", 40);
        context.set("dkw", 50);
        Object evaluate = jexlEngine.createExpression(expressionString).evaluate(context);

        System.out.println(evaluate);
    }
}
